package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.entity.SysRole;
import com.taxx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色管理控制器
 */
@RestController
@RequestMapping("/roles")
@CrossOrigin
public class SysRoleController {
    
    @Autowired
    private SysRoleService roleService;
    
    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    public Result<List<SysRole>> getRoleList() {
        try {
            List<SysRole> roles = roleService.getRoleList();
            return Result.success("获取角色列表成功", roles);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取角色信息
     */
    @GetMapping("/{id}")
    public Result<SysRole> getRoleById(@PathVariable Long id) {
        try {
            SysRole role = roleService.getRoleById(id);
            if (role == null) {
                return Result.notFound("角色不存在");
            }
            return Result.success("获取角色信息成功", role);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 创建角色
     */
    @PostMapping
    public Result<Void> createRole(@Validated @RequestBody SysRole role) {
        try {
            boolean success = roleService.createRole(role);
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色创建失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    public Result<Void> updateRole(@PathVariable Long id, @Validated @RequestBody SysRole role) {
        try {
            role.setId(id);
            boolean success = roleService.updateRole(role);
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteRole(@PathVariable Long id) {
        try {
            boolean success = roleService.deleteRole(id);
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色删除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新角色状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateRoleStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = roleService.updateRoleStatus(id, status);
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色状态更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 分配角色权限
     */
    @PostMapping("/{id}/permissions")
    public Result<Void> assignPermissions(@PathVariable Long id, @RequestBody List<Long> permissionIds) {
        try {
            boolean success = roleService.assignPermissions(id, permissionIds);
            if (success) {
                return Result.success();
            } else {
                return Result.error("权限分配失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 分配角色菜单
     */
    @PostMapping("/{id}/menus")
    public Result<Void> assignMenus(@PathVariable Long id, @RequestBody List<Long> menuIds) {
        try {
            boolean success = roleService.assignMenus(id, menuIds);
            if (success) {
                return Result.success();
            } else {
                return Result.error("菜单分配失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
